import React from 'react'
// ===================================================================== Temp
import FormModal from '#cpt/temp/FormModal'
// ===================================================================== global declare
const { $fn, $http, $Lazy } = window
// ===================================================================== $Lazy
const $cache = $Lazy.file(()=>import('#private/cache-api'))
// ===================================================================== component
const afterText = '注：一个第三方条码只能绑定10个条码'
const boolStatus = [{value:'1',name:'是'},{value:'0',name:'否'}]
class Index extends React.Component{
	state = {
		data: [],
		// 绑定第三方条码
		formData:[
			{ label: '当前条码', 		name:'old_spec_code', 		full:true,	type:'',		required:true,		hide:false,	afterText:'' },
			{ label: '第三方条码', 		name:'third_spec_code', 	full:true,					required:true },
			{ label: '第三方来源', 		name:'third_source_type', 	full:true, 	type:'select',	idStr:'code' },
			{ label: '开启第三方上架',	name: 'third_open_shelf', 	full:true, 	type: 'radio',	data:boolStatus, value:'0',
				onValue: ({value,formData}) => this.thirdOpen(value,formData)
			},
			{ label: '第三方医院', 		name:'third_hosp_id', 		full:true, 	type:'select' },
		]
	}	
	submit = {}
	spec_codes = {}
	/* 修改绑定第三方条码可批量操作 2021-8-12 11:41:05 --马婷 */
	open = (rows)=> {
		const {formData} = this.state
		let spec_code = rows.map(v => v.spec_code)
		this.spec_codes = spec_code = Array.from(new Set(spec_code))
		if(spec_code.length === 1){
			this.formRef.setValue({third_open_shelf: '1',old_spec_code:spec_code[0] })
			this.thirdOpen('1',formData)
		}else{
			this.formRef.setValue({third_open_shelf: '0',old_spec_code:spec_code.join('\r') })
			this.thirdOpen('0',formData)
		}
		this.getModalSelect()
		if(spec_code.length){
			$http.submit(null,'specimen/thirdInfo',{ param:{spec_code:spec_code[0]}}).then(({third_spec_code, third_source_type, third_hosp_id})=>{
				this.formRef.setValue({third_spec_code, third_source_type, third_hosp_id})
			})
		}
		this.modal.open({formRef: this.formRef})
	}
	// 弹窗获取下拉数据
	getModalSelect = () => {
		const { formData } = this.state
		$cache.then( async f => {
			// 第三方医院
			$fn.setRow( formData, 'third_hosp_id',  await f.getCache({ api:'hospital' }))
			// 第三方来源
			$fn.setRow( formData, 'third_source_type', await f.getDicCache({ code:69000 }) )
			this.setState({ formData })
		})
	}
	thirdOpen = (value,formData) => {
		const {spec_codes} = this
		if(Boolean(+value)){
			formData[0].type = 'input'
			formData[0].afterText = ''
			formData[2].required = true
			formData[4].required = true
			this.setState({formData})
			if(spec_codes.length){
				this.formRef.setValue({old_spec_code:spec_codes[0]})
			}else{
				this.formRef.setValue({old_spec_code:''})
			}
		}else{
			formData[0].type = 'textarea'
			formData[0].afterText = afterText
			formData[2].required = false
			formData[4].required = false
			this.setState({formData})
			if(spec_codes.length){
				this.formRef.setValue({old_spec_code:spec_codes.join('\r')})
			}else{
				this.formRef.setValue({old_spec_code:''})
			}
		}
	}
	
	render(){
		return FormModal.call(this, {title:'绑定第三方条码', labelWidth:'s8', width:800, api:'',enter:false,onOk:this.getSpec,
			onSubmit: ()=> {
				let datas = this.formRef.getValue()
				let {old_spec_code} = datas
				if(old_spec_code){
					old_spec_code = old_spec_code.match(/\d{14}|\d{12}/g)
					if(old_spec_code.length === 1){
						old_spec_code = Array.from(new Set(old_spec_code))
					}else{
						old_spec_code = Array.from(new Set(old_spec_code))
						if(old_spec_code.length > 10){
							return $fn.msg.warning('一个第三方条码只能绑定10个条码')
						}
					}
				}
				$http.submit(null,'specimen/bindThirdSpec',{ param:{...datas,old_spec_code }}).then(data=>{
					$fn.msg.success('操作成功')
					this.modal.close()
					this.props.onRefresh()
				})
			}
		} )
	}
}

export default Index